<?php
	require '../fckeditor/fckeditor.php';
	class MainControl{
		private $smarty;
		private $timer;
		
	function __construct($smarty,$action=null){
			$this->timer=new Timer();
			$this->smarty=$smarty;
			switch($action) {
				//用户管理部分
				//账号管理
				case 'addUser':
					$this->addUser();
					break;
				case 'editUser':
					$this->editUser();
					break;
				//系统信息
				case 'sysInfo':
					$this->sysInfo();
					break;
				case 'updateCache':
					$this->updateCache();
					break;
					
				//任务管理部分
				//任务信息
				case 'addMission':
					$this->addMission();
					break;
				case 'editMission':
					$this->editMission();
					break;
				case 'ckMission':
					$this->ckMission();
					break;

				//商品管理部分
				//商品信息
				case 'addProduct':
					$this->addProduct();
					break;
				case 'editProduct':
					$this->editProduct();
					break;
				//订单信息
				case 'ckForm':
					$this->ckForm();
					break;
				case 'confirm':
					$this->confirm();
					break;
				default:
					$this->sysIntroduce();	
			}
		}
		//增加用户
		private function addUser(){
			$this->smarty->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息. ');
			$user=new User();
			if($_GET["edit"]=="mod"){
				$this->smarty->assign("dtitle", '用户管理>账号管理>修改用户');
				$data=$user->getUser($_GET["id"]);
				$this->smarty->assign("post", $data);
				$this->smarty->assign("act","mod");
				$this->smarty->assign("action", "addUser&edit=mod&id=".$_GET["id"]);
				$this->smarty->assign("submitButton", "修 改");
			}else{
				$this->smarty->assign("dtitle", '用户管理>账号管理>添加用户');
				$this->smarty->assign("action", "addUser&edit=add");
				$this->smarty->assign("act","add");
				$this->smarty->assign("submitButton", "添 加");
			}
		
		
			if($_POST["action"]=="mod"){
				if($user->validateForm(0)){
					if($user->modUser($_POST)){
						echo '<script>window.location="main.php?action=editUser"</script>';
						return;
					}else{
						$this->message(0, $user->getMessList());
					}
				}else{
					$this->message(0, $user->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
		
				
			if($_POST["action"]=="add"){
				if($user->validateForm()){
					if($user->userAdd($_POST)){
						$this->message(1,$user->getMessList());
						$_POST=array();
					}else{
						$this->message(0, $user->getMessList());
					}
				}else{
					$this->message(0,$user->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
			$this->smarty->display("admin/addUser.html");
		}
		
		private function editUser(){
			$this->smarty->assign("dtitle", '用户管理>账号管理>编辑用户');
			$this->smarty->assign("tmess", '用户被删除时,他的相关记录也会一并删除,请谨慎操作. ');
			$user=new User();
				
		
			if($_GET["edit"]=="del"){
				if($user->delUser($_REQUEST["id"])){
					$this->message(1,$user->getMessList());
				}else{
					$this->message(0, $user->getMessList());
				}
			}
		
			if(isset($_POST["dels_x"]) || isset($_POST["dels_y"])){
				if($user->delUser($_POST["del"])){
					$this->message(1,$user->getMessList());
				}else{
					$this->message(0, $user->getMessList());
				}
			}
		
			$total=$user->getRowTotal();
			$current_page=isset($_GET["page"])?$_GET["page"]:1;
			$pageObj=new Page($total, $current_page, MISSION_PAGE_SIZE);
			$pageInfo=$pageObj->getPageInfo();
			$allUsers=$user->getAllUsers($pageInfo["row_offset"], $pageInfo["row_num"]);
			$this->smarty->assign("users", $allUsers);
			$this->smarty->assign("pageInfo", $pageInfo);
		
			$this->smarty->display("admin/editUser.html");
		}
		
		private function sysInfo(){
			$mydb=new MyDB();
			$sysinfo=new SysInfo($mydb);
			$this->smarty->assign("sysinfo", $sysinfo->getSysInfos());
			$this->smarty->assign("dtitle", '用户管理>系统信息>系统信息');
			$this->smarty->assign("tmess", '可以根据网站的需要，对服务器现有的配置进行调整.');
			$this->smarty->display("admin/sysInfo.html");
		}
		
		private function updateCache(){
			$this->smarty->assign("dtitle", '用户管理>系统信息>更新缓存');
				
			$this->message(1,"缓存更新成功！");
			$this->smarty->clearAllCache();
			$this->smarty->display("admin/updateCache.html");
		}
		
		private function message($bool, $message){
			if($bool){
				$this->smarty->assign("mess", "sucess");
				$this->smarty->assign("tmess", $message);
			}else{
				$this->smarty->assign("mess", "error");
				$this->smarty->assign("tmess", $message);
			}
		}
		
		private function sysIntroduce(){
			$this->smarty->assign("dtitle", '后台管理首页');
			$this->smarty->assign("tmess", '请操作左侧菜单，对本网站进行管理.');
			$this->smarty->display("admin/main.html");
		}
		
		private function addMission(){
			$this->smarty->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息. ');
			$mission=new Mission();
			$fckeditor=new FCKeditor("content");
			$fckeditor->BasePath="../fckeditor/";
			
		if($_GET["edit"]=="mod"){
				$this->smarty->assign("dtitle", '任务管理>任务积分管理>修改任务');
				$data=$mission->getMission($_GET["id"]);
				$this->smarty->assign("post", $data);
				$this->smarty->assign("act","mod");
				$this->smarty->assign("action", "addMission&edit=mod&id=".$_GET["id"]);
				$this->smarty->assign("submitButton", "修 改");
			}else{
				$this->smarty->assign("dtitle", '任务管理>任务积分管理>添加任务');
				$this->smarty->assign("action", "addMission&edit=add");
				$this->smarty->assign("act","add");
				$this->smarty->assign("submitButton", "添 加");
			}
			
			
			if($_POST["action"]=="mod"){
				if($user->validateForm(0)){
					if($mission->modMission($_POST)){
						echo '<script>window.location="main.php?action=editMission"</script>';
						return;
					}else{
						$this->message(0, $mission->getMessList());
					}
				}else{
					$this->message(0, $mission->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
			
			
			if($_POST["action"]=="add"){
				if($mission->validateForm()){
					if($mission->addMission($_POST)){
						$this->message(1,$mission->getMessList());
						$_POST=array();
					}else{
						$this->message(0, $mission->getMessList());
					}
				}else{
					$this->message(0,$mission->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
			
			$this->smarty->assign("fckeditor",$fckeditor->CreateHtml());
			$this->smarty->display("admin/addMission.html");
		}
		
		private function editMission(){
			$this->smarty->assign("dtitle", '任务管理>任务积分管理>编辑任务');
			$this->smarty->assign("tmess", '任务被删除时,他的相关记录也会一并删除,请谨慎操作. ');
			$mission=new Mission();
			
			
			if($_GET["edit"]=="del"){
				if($mission->delMission($_REQUEST["id"])){
					$this->message(1,$mission->getMessList());
				}else{
					$this->message(0, $mission->getMessList());
				}
			}
			
			if(isset($_POST["dels_x"]) || isset($_POST["dels_y"])){
				if($mission->delMission($_POST["del"])){
					$this->message(1,$mission->getMessList());
				}else{
					$this->message(0, $mission->getMessList());
				}
			}
			
			$total=$mission->getRowTotal();
			$current_page=isset($_GET["page"])?$_GET["page"]:1;
			$pageObj=new Page($total, $current_page, ARTICLE_PAGE_SIZE);
			$pageInfo=$pageObj->getPageInfo();
			$allUsers=$mission->getAllUsers($pageInfo["row_offset"], $pageInfo["row_num"]);
			$this->smarty->assign("missions", $allUsers);
			$this->smarty->assign("pageInfo", $pageInfo);
			
			$this->smarty->display("admin/editUser.html");
		}
		
		private function ckMission(){
			
		}
		
		private function addProduct(){
			$this->smarty->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息. ');
			$mission=new Mission();
			$fckeditor=new FCKeditor("content");
			$fckeditor->BasePath="../fckeditor/";
				
			if($_GET["edit"]=="mod"){
				$this->smarty->assign("dtitle", '任务管理>任务积分管理>修改任务');
				$data=$mission->getMission($_GET["id"]);
				$this->smarty->assign("post", $data);
				$this->smarty->assign("act","mod");
				$this->smarty->assign("action", "addMission&edit=mod&id=".$_GET["id"]);
				$this->smarty->assign("submitButton", "修 改");
			}else{
				$this->smarty->assign("dtitle", '任务管理>任务积分管理>添加任务');
				$this->smarty->assign("action", "addMission&edit=add");
				$this->smarty->assign("act","add");
				$this->smarty->assign("submitButton", "添 加");
			}
				
				
			if($_POST["action"]=="mod"){
				if($user->validateForm(0)){
					if($mission->modMission($_POST)){
						echo '<script>window.location="main.php?action=editMission"</script>';
						return;
					}else{
						$this->message(0, $mission->getMessList());
					}
				}else{
					$this->message(0, $mission->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
				
				
			if($_POST["action"]=="add"){
				if($mission->validateForm()){
					if($mission->addMission($_POST)){
						$this->message(1,$mission->getMessList());
						$_POST=array();
					}else{
						$this->message(0, $mission->getMessList());
					}
				}else{
					$this->message(0,$mission->getMessList());
				}
				$this->smarty->assign("post", $_POST);
			}
				
			$this->smarty->assign("fckeditor",$fckeditor->Create());
			$this->smarty->display("admin/addMission.html");
		}
		
		private function editProduct(){
			
		}
		
		private function ckForm(){
			
		}
		
		private function confirm(){
			
		}
		
		function __destruct(){
			$this->timer->stop();
			$this->smarty->assign("timer", $this->timer->spent());
			$this->smarty->display("admin/timer.html");
		}
	}
?>